import FRequest from '@/library/FRequest'


const $request = new FRequest()
$request.config.baseUrl = "http://localhost:3000"
$request.config.isJson = true
$request.config.headers = {
    'content-type': 'application/json'
}
$request.interceptor.response.convert = (res) => {
    const contentType = res.headers.get('content-type')
    if (contentType.includes('application/json')) {
        return res.json()
    }
    return res.text()
}
$request.interceptor.response.text = (res) =>{
    switch (res.code) {
        case 300:
        console.log(res.message);
            break;
        case 400:
        console.log('未登录');
            break;
    }
}
$request.interceptor.request = (init) => {
    return new Promise((resolve) => {
        if (window.isLogin) {
            let headers = init.headers ?? {}
            if (window.loginData === null) {
                tools.ipcInvoke('readFileSync', './../../loginData.json')
                .then( fileData => {
                    window.loginData = JSON.parse(fileData)
                    console.log(fileData, 'fileData---');
                    headers = {
                        ...headers,
                        ...window.loginData
                    }
                    init.headers = headers;
                    resolve(init)
                })
            } else {
                
                headers = {
                    ...headers,
                    ...window.loginData
                }
                init.headers = headers;
                resolve(init)
            }
            
        } else {
            resolve(init)
        }
        
    })
    
    
    

    
    // if (userId !== null && time !== null && token !== null) {
    //     loginData = {
    //         'x-id': userId,
    //         'x-time': time,
    //         'x-token': token,
    //     }
    // }
    
}

export default $request